package leetcode.day_2021_03_22_2021_03_28;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 73. 矩阵置零
 * https://leetcode-cn.com/problems/set-matrix-zeroes/
 */
public class LeetCode0073 {

    public static void setZeroes(int[][] matrix) {
        int rows = matrix.length;
        int cols = matrix[0].length;

        List<int[]> zero = new ArrayList<>();
        for (int row=0; row<rows; row++) {
            for (int col = 0; col<cols; col++) {
                if (matrix[row][col] == 0){
                    zero.add(new int[]{row, col});
                }
            }
        }

        if (zero.size() != 0) {
            for (int i=0; i<zero.size(); i++) {
                setZero(zero.get(i)[0], zero.get(i)[1], rows, cols, matrix);
            }
        }
    }

    public static void setZero(int row, int col, int rows, int cols, int[][] matrix){
        for (int i=0; i<rows; i++) {
            matrix[i][col] = 0;
        }

        for (int j=0; j<cols; j++) {
            matrix[row][j] = 0;
        }
    }

    public static void main(String[] args) {
        int[][] matrix = {{0,1,2,0}, {3,4,5,2},{1,3,1,5}};
        setZeroes(matrix);
        System.out.println(Arrays.deepToString(matrix));
    }
}
